package com.fan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fan.dao.PlayHistoryDao;
import com.fan.entity.PlayHistory;
import com.fan.service.PlayHistoryService;
import com.sixth.until.CommonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: self-tui-education
 * @description:
 * @author: 老范
 * @create: 2022-05-08 18:38
 **/
@RestController
@RequestMapping("/order/play-history")
public class PlayHistoryController {
    @Autowired
    private PlayHistoryService playHistoryService;
    @Autowired
    private PlayHistoryDao playHistoryDao;




    //    插入一条播放历史信息表
    @PostMapping("/insertPlayHistoryByCourseId/{courseID}/{userId}/{vedeo_id}")
    @ApiOperation(value = "插入或者更新消息(tb_play_history)")
    public CommonResult insertPlayHistoryByCourseId(@PathVariable("courseID") String courseID, @PathVariable("userId") String userId,@PathVariable("vedeo_id") String vedeoId){
        QueryWrapper<PlayHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("vedeo_id",vedeoId);
        PlayHistory selectOne = playHistoryDao.selectOne(wrapper);
//        System.out.println("查询的数据"+selectOne);
        CommonResult commonResult=null;
        if (selectOne==null){
//            插入
            commonResult = playHistoryService.insertPlayHistoryByCourseId(vedeoId, userId);
            commonResult=new CommonResult(2000,"插入成功",commonResult);

        }else{
//            更新
            commonResult = playHistoryService.updatePlayHistory(userId,vedeoId);
            commonResult=new CommonResult(2000,"更新成功",commonResult);

        }

        return commonResult;
    }




    //    查询所有的播放信息
    @PostMapping("/queryPlayHistoryBy/{page}/{limit}/{HuiYuanId}")
    @ApiOperation(value = "查询tb_play_history")
    public CommonResult queryPlayHistoryBy(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, @PathVariable("HuiYuanId") String HuiYuanId, @RequestBody PlayHistory playHistory){
        CommonResult commonResult = playHistoryService.queryPlayHistory(page,limit,HuiYuanId,playHistory);
        return commonResult;
    }
}
